import requests
from lxml import etree
import bs4
# 请求的地址
url = "https://www.lmonkey.com/essence"
# 请求头headers
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'
}
# 发送请求
res = requests.get(url=url, headers=headers)

#soup = bs4.BeautifulSoup(res.text, 'html.parser')
# 匹配数据
#results = soup.find_all('div', "list-group-item list-group-item-action px-0 xin_hover")

if res.status_code == 200:
    # 请求的内容写入文件
    with open("./test.html", "w") as fp:
        fp.write(res.text)
# 解析数据
html = etree.parse("./test.html", etree.HTMLParser())
# 提取数据 作者 文章
# authors = html.xpath('//div[@class="topic_title"]//div[@class="list-group-item"]//strong/a/text()')
titles = html.xpath('//div[@class="list-group-item list-group-item-action px-0 xin_hover"]//div[@class="topic_title mb-0  essence_title yh"]/text()')
authors = html.xpath('//div[@class="list-group-item list-group-item-action px-0 xin_hover"]//p[@class="lh-140 mb-0 topic_info pt-3"]/strong/a/text()')
titleurl = html.xpath('//div[@class="list-group-item list-group-item-action px-0 xin_hover"]//a/@href')
data = []
for i in range(0, len(authors)):
    res = {"author":authors[i], "title": titles[i], "url":titleurl[i]}
    data.append(res)
print(data)